package main

import (
	"fmt"
	"interstellar/zinx/utils"
	"interstellar/zinx/znet"
	"io"
	"net"
	"time"
)

/*
 模拟客户端
*/
func main() {
	fmt.Println("client0 start... ")

	time.Sleep(1 * time.Second)
	// 1 直接链接远程服务器，得到一个conn链接

	conn, err := net.Dial("tcp", fmt.Sprintf("%s:%d", utils.GlobalObject.Host, utils.GlobalObject.TcpPort))
	if err != nil {
		fmt.Println("client start err,exit !")
		return
	}

	for {
		////2 链接调用Write  写数据
		//_, err := conn.Write([]byte("Hello Zinx Vo.1."))
		//if err != nil {
		//	fmt.Println("write conn err", err)
		//	return
		//}
		//buf := make([]byte, utils.GlobalObject.MaxPackageSize)
		//cnt, err := conn.Read(buf)
		//if err != nil {
		//	fmt.Println("read buf error")
		//	return
		//}
		//
		//fmt.Printf(" server call back: %s, cnt=%d\n", buf, cnt)

		// 发送封包的message消息 MsgID:0
		dp := znet.NewDataPack()
		binaryMsg, err := dp.Pack(znet.NewMsgPackage(0, []byte("Zinx client0 Test Message")))
		if err != nil {
			fmt.Println("Pack error: ", err)
			return
		}
		if _, err := conn.Write(binaryMsg); err != nil {
			fmt.Println("write error ", err)
			return
		}
		// 服务器就应该给我们回复一个message数据，MsgID:1 ping..ping...ping

		//1 先读取流中的head部分 得到ID和dataLen
		binaryHead := make([]byte, dp.GetHeadLen())
		if _, err := io.ReadFull(conn, binaryHead); err != nil {
			fmt.Println("read head error ", err)
			break
		}
		// 将二进制的head拆包到msg 结构体中
		msgHead, err := dp.Unpack(binaryHead)
		if err != nil {
			fmt.Println("client unpack msgHead error ", err)
			break
		}
		if msgHead.GetMsgLen() > 0 {
			//2 再根据dataLen进行第二次读取，奖data读出来
			msg := msgHead.(*znet.Message)
			msg.Data = make([]byte, msg.GetMsgLen())
			if _, err := io.ReadFull(conn, msg.Data); err != nil {
				fmt.Println("read msg data error , ", err)
				return
			}

			fmt.Println("---> Recv Server Msg : ID = ", msg.Id, ", len = ",
				msg.DataLen, ", data = ", string(msg.Data))
		}

		//cpu阻塞
		time.Sleep(1 * time.Second)
	}
}
